package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.socket.MessageType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.socket.bean.BaseDataPiece;
import com.ruoyi.common.core.domain.entity.socket.bean.BoatMessage;
import com.ruoyi.common.core.domain.entity.socket.bean.ShiplineInfo;
import com.ruoyi.common.core.domain.entity.system.SysShipline;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.handler.socket.SocketMessageHandler;
import com.ruoyi.manager.SocketChannelManager;
import com.ruoyi.system.service.ISysShiplineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 自动航线Controller
 *
 * @author zjw
 * @date 2023-07-05
 */
@RestController
@RequestMapping("/system/shipline")
public class SysShiplineController extends BaseController {
    @Autowired
    private ISysShiplineService sysShiplineService;

    /**
     * 查询自动航线列表
     */
//    @PreAuthorize("@ss.hasPermi('system:shipline:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(SysShipline sysShipline) {
//        startPage();
//        List<SysShipline> list = sysShiplineService.selectSysShiplineList(sysShipline);
//        return getDataTable(list);
//    }
//    @PreAuthorize("@ss.hasPermi('system:shipline:list')")
    @GetMapping("/list")
    public AjaxResult list(SysShipline sysShipline) {
        List<SysShipline> list = sysShiplineService.selectSysShiplineList(sysShipline);
        return success(list);
    }

    /**
     * 导出自动航线列表
     */
    @PreAuthorize("@ss.hasPermi('system:shipline:export')")
    @Log(title = "自动航线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysShipline sysShipline) {
        List<SysShipline> list = sysShiplineService.selectSysShiplineList(sysShipline);
        ExcelUtil<SysShipline> util = new ExcelUtil<SysShipline>(SysShipline.class);
        util.exportExcel(response, list, "自动航线数据");
    }

    /**
     * 获取自动航线详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shipline:query')")
    @GetMapping(value = "/{shiplineId}")
    public AjaxResult getInfo(@PathVariable("shiplineId") Long shiplineId) {
        return success(sysShiplineService.selectSysShiplineByShiplineId(shiplineId));
    }

    /**
     * 新增自动航线
     */
    @PreAuthorize("@ss.hasPermi('system:shipline:add')")
    @Log(title = "自动航线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysShipline sysShipline) {
        String[] coordinateArr = sysShipline.getCoordinates().split(",");
        sysShipline.setCoordinateNum((short) (coordinateArr.length / 2));

        sysShipline.setCreateBy(getUsername());
        sysShipline.setCreateTime(DateUtils.getNowDate());

        int rows = sysShiplineService.insertSysShipline(sysShipline);

        return toAjax(rows);
    }


    /**
     * 修改自动航线
     */
    @PreAuthorize("@ss.hasPermi('system:shipline:edit')")
    @Log(title = "自动航线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysShipline sysShipline) {
        String[] coordinateArr = sysShipline.getCoordinates().split(",");
        sysShipline.setCoordinateNum((short) (coordinateArr.length / 2));
        sysShipline.setUpdateBy(getUsername());
        sysShipline.setUpdateTime(new Date());

        int rows = sysShiplineService.updateSysShipline(sysShipline);
        if (rows > 0) { //自动航线更新成功
            SocketMessageHandler.sendShiplineInfoToAllOnlineBoat(sysShipline);
        }

        return toAjax(rows);
    }

    /**
     * 删除自动航线
     */
    @PreAuthorize("@ss.hasPermi('system:shipline:remove')")
    @Log(title = "自动航线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scenicId}/{shiplineIds}")
    public AjaxResult remove(@PathVariable("scenicId") Long scenicId, @PathVariable Long[] shiplineIds) {
        int num = sysShiplineService.deleteSysShiplineByShiplineIds(shiplineIds);
        if (num > 0) {
            BoatMessage boatMessage = new BoatMessage(MessageType.CONFIG_MESSAGE, (byte) 1);
            ShiplineInfo shiplineInfo = new ShiplineInfo();
            shiplineInfo.setCreateOrUpdateTime(new Date());
            shiplineInfo.setCoordinateNum((short) 0);

            List<BaseDataPiece> dataPieceList = boatMessage.getDataPieceList();
            dataPieceList.add(shiplineInfo);

            SocketChannelManager.sendMsgToAllOnlineBoatOfAssignedScenic(scenicId, boatMessage);
        }
        return toAjax(num);
    }
}
